package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.*;
import com.qf.pojo.*;
import com.qf.service.DtsAdminGoodsService;
import com.qf.service.DtsGoodsAttributeService;
import com.qf.service.DtsGoodsProductService;
import com.qf.service.DtsGoodsSpecificationService;
import com.qf.util.ResponseUtil;
import com.qf.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class DtsAdminGoodsServiceImpl implements DtsAdminGoodsService {

    @Autowired
    DtsGoodsMapper goodsMapper;
    @Override
    public IPage<DtsGoods> queryPage(Integer page, Integer limit, String sort, String order, String goodsSn, String name, List<Integer> brandId) {
        IPage<DtsGoods> iPage = new Page<>(page,limit);
        QueryWrapper<DtsGoods> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }

        if(!StringUtils.isEmpty(goodsSn)){
            qw.like("goods_sn",goodsSn);
        }

        if(!StringUtils.isEmpty(name)){
            qw.like("name",name);
        }

        if(!CollectionUtils.isEmpty(brandId)){
            qw.in("brand_id",brandId);
        }

        goodsMapper.selectPage(iPage,qw);
        return iPage;
    }

    @Autowired
    DtsCategoryMapper categoryMapper;
    @Autowired
    DtsGoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    DtsGoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    DtsGoodsProductMapper goodsProductMapper;

    @Override
    public Map<String, Object> queryDetailById(Integer id) {
        Map<String,Object> data = new HashMap<>();
        //goods
        DtsGoods goods = goodsMapper.selectById(id);
        //categoryIds
        DtsCategory category = categoryMapper.selectById(goods.getCategoryId());
        //attributes
        LambdaQueryWrapper<DtsGoodsAttribute> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(DtsGoodsAttribute::getGoodsId,id);
        queryWrapper1.eq(DtsGoodsAttribute::getDeleted,false);
        List<DtsGoodsAttribute> goodsAttributes = goodsAttributeMapper.selectList(queryWrapper1);
        //specifications
        LambdaQueryWrapper<DtsGoodsSpecification> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(DtsGoodsSpecification::getGoodsId,id);
        queryWrapper2.eq(DtsGoodsSpecification::getDeleted,false);
        List<DtsGoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectList(queryWrapper2);
        //products
        LambdaQueryWrapper<DtsGoodsProduct> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(DtsGoodsProduct::getGoodsId,id);
        queryWrapper3.eq(DtsGoodsProduct::getDeleted,false);
        List<DtsGoodsProduct> goodsProducts = goodsProductMapper.selectList(queryWrapper3);

        data.put("categoryIds", Arrays.asList(goods.getCategoryId(),category.getPid()));
        data.put("goods",goods);
        data.put("attributes",goodsAttributes);
        data.put("specifications",goodsSpecifications);
        data.put("products",goodsProducts);
        return data;
    }

    @Autowired
    DtsBrandMapper brandMapper;


    public Object estimate(GoodsAllinone goodsAllinone){
        DtsGoods goods = goodsAllinone.getGoods();
        if(goods == null){
            return ResponseUtil.badArgument();
        }

        if(goods.getName() == null){
            return ResponseUtil.fail(-1,"商品名称不能为空");
        }

        if(goods.getGoodsSn() == null){
            return ResponseUtil.fail(-1,"商品编号不能为空");
        }

        Integer brandId = goods.getBrandId();
        LambdaQueryWrapper<DtsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DtsBrand::getDeleted,false);
        queryWrapper.eq(DtsBrand::getId,brandId);
        DtsBrand dtsBrand = brandMapper.selectOne(queryWrapper);
        if(dtsBrand == null){
            return ResponseUtil.fail(-1,"品牌商不存在");
        }

        Integer categoryId = goods.getCategoryId();
        DtsCategory dtsCategory = categoryMapper.selectById(categoryId);
        if(dtsCategory == null){
            return ResponseUtil.fail(-1,"该分类不存在");
        }

        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        if(attributes == null || attributes.length == 0){
            return ResponseUtil.fail(-1,"商品属性不能为空");
        }

        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        if(specifications == null || specifications.length == 0){
            return ResponseUtil.fail(-1,"商品规格不能为空");
        }

        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        if(products == null || products.length == 0){
            return ResponseUtil.fail(-1,"商品库存不能为空");
        }

        return null;
    }

    @Autowired
    DtsOrderGoodsMapper orderGoodsMapper;

    @Autowired
    DtsGoodsSpecificationService goodsSpecificationService;

    @Autowired
    DtsGoodsAttributeService goodsAttributeService;

    @Autowired
    DtsGoodsProductService goodsProductService;

    @Override
    public Object update(GoodsAllinone goodsAllinone) {
        Object result = this.estimate(goodsAllinone);
        if(result != null){
            return result;
        }
        DtsGoods goods = goodsAllinone.getGoods();
//        LambdaQueryWrapper<DtsOrderGoods> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(DtsOrderGoods::getGoodsId,goods.getId());
//        queryWrapper.eq(DtsOrderGoods::getDeleted,false);
//        List<DtsOrderGoods> dtsOrderGoods = orderGoodsMapper.selectList(queryWrapper);
//        if(dtsOrderGoods == null || dtsOrderGoods.size() == 0){
//            return ResponseUtil.fail(-1,"还有订单未完成");
//        }

        goods.setUpdateTime(new Date());
        goodsMapper.updateById(goods);

        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        goodsSpecificationService.update(goods.getId(),specifications);

        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        goodsAttributeService.update(goods.getId(),attributes);

        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        goodsProductService.update(goods.getId(),products);
        return ResponseUtil.ok();
    }

    @Autowired
    StatMapper statMapper;

    @Override
    public Object delete(DtsGoods goods) {
        long count = statMapper.queryOrderCountById(goods.getId());
        if(count > 0){
            return ResponseUtil.fail(-1,"与该商品还有关联的订单未完成");
        }
        goods.setDeleted(true);
        goodsMapper.updateById(goods);

        goodsAttributeService.delete(goods.getId());

        goodsProductService.delete(goods.getId());

        goodsSpecificationService.delete(goods.getId());
        return ResponseUtil.ok();
    }

    @Override
    public Object create(GoodsAllinone goodsAllinone) {

        Object flag = this.estimate(goodsAllinone);
        if(flag != null){
            return flag;
        }

        DtsGoods goods = goodsAllinone.getGoods();
        String name = goods.getName();
        LambdaQueryWrapper<DtsGoods> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoods::getName,name);
        Integer count = goodsMapper.selectCount(qw);
        if(count > 0){
            return ResponseUtil.fail(-1,"该商品名称已被注册");
        }

        goods.setDeleted(false);
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        goodsMapper.insert(goods);

        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        goodsAttributeService.insert(goods.getId(),attributes);
        goodsProductService.insert(goods.getId(),products);
        goodsSpecificationService.insert(goods.getId(),specifications);
        return ResponseUtil.ok();
    }
}
